<template>
  <div class="container">
    <a-spin size="large" :tip="tip">
      <div class="container"></div>
    </a-spin>
  </div>
</template>
<script lang="ts" setup>
import { useStore } from "vuex";
import { useRouter } from "vue-router";
// import qs from "qs";
import { SYS_TOKEN } from "@/utils/global-constant";
import LocalCache from "@/utils/cache";
import { login, getUserInfo } from "@/service/modules/login/login";
import { getQueryString } from "@/utils/utils";
import { APPID } from "@/service/request/config";

const tip = "加载中...";
const store = useStore();
const router = useRouter();
// const route = useRoute();
//模拟接口菜单数据
const menuList = [
  {
    spyCode: "zbb_02",
    title: "值班表管理",
  },
  {
    spyCode: "zbb_03",
    title: "值班记录管理",
  },
];
//系统初始化
doLogin();
// 登录-获取用户信息-获取菜单
async function doLogin() {
  const token =
    (LocalCache.getCookie("userInfo") &&
      JSON.parse(LocalCache.getCookie("userInfo")).token) ||
    "7B37A8DF2A184B9D96486D0FA6A5CCFF";
  const orgCode =
    (LocalCache.getCookie("userInfo") &&
      JSON.parse(LocalCache.getCookie("userInfo")).orgCode) ||
    "scsslytzx";
  const accessToken =
    (LocalCache.getCookie("userInfo") &&
      JSON.parse(LocalCache.getCookie("userInfo")).accessToken) ||
    "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI0NjcyODMwNjc3MzIxNzY4OTciLCJpYXQiOjE3MDAyMDgxMzgsImFjY291bnRObyI6IjRGZVNrIiwibG9naW5EZXZpY2UiOiJtaW5pYXBwIiwiZXhwIjoxNzAwMjk0NTM4fQ.Jm7hROhSeFpQkHzOeYvcxmIjObzvbrQ1VtaVggikdAdP6eYMDg2mcyn3ZJGpbv5uk_l_Ix8GLyNTmfJIBXnWID144MjFigZMGANV1zypw99GxkRvz2NnRUO3IMOIImROxbpWZFprBbEhMH3PI24G5W_LS2vzQkIx_CJM9FKKtVs";
  LocalCache.setSession("orgToken", getQueryString("token") || token);
  LocalCache.setSession("orgCode", getQueryString("orgCode") || orgCode);

  const params = {
    orgCode: getQueryString("orgCode") || orgCode,
    token: getQueryString("token") || token,
    accessToken: getQueryString("accessToken") || accessToken,
    appId: APPID,
  };
  const loginResult = await login(params);
  if (loginResult?.code === 0) {
    LocalCache.setSession(SYS_TOKEN, loginResult?.token);
    const userInfo = await getUserInfo();
    if (userInfo?.code === 0) {
      LocalCache.setSession("userInfo", userInfo?.data);
    }
    store.commit("common/gettedInfo", true);
    getMenu();
    router.push({
      path: "/home",
    });
  }
}

// 获取菜单信息
function getMenu() {
  if (!(window as any).__POWERED_BY_QIANKUN__) {
    store.dispatch("addRoutes/actionMenu", menuList);
  }
}
</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100vh;
}
</style>
